﻿using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;

using Supremacy.Resources;
using Supremacy.VFS;

namespace SupremacyEditor
{
    public class EditorModule : IModule
    {
        #region Fields
        private readonly IUnityContainer _container;
        private readonly IPathDiscoveryService _pathDiscoveryService;
        private readonly IRegionManager _regionManager;
        private readonly IResourceManagerService _resourceManagerService;
        private readonly IVFSService _vfsService;
        #endregion

        #region Constructors
        public EditorModule(
            IUnityContainer container,
            IRegionManager regionManager,
            IResourceManagerService resourceManagerService,
            IPathDiscoveryService pathDiscoveryService,
            IVFSService vfsService)
        {
            _container = container;
            _regionManager = regionManager;
            _resourceManagerService = resourceManagerService;
            _pathDiscoveryService = pathDiscoveryService;
            _vfsService = vfsService;
        }
        #endregion

        #region Methods
        protected void RegisterViewsAndServices()
        {
            _container.RegisterType<IGameDataModel, GameDataModel>();
        }
        #endregion

        #region IModule Members
        public void Initialize()
        {
            RegisterViewsAndServices();

            var currentMod = _resourceManagerService.CurrentMod;
            if (currentMod != null)
                _vfsService.AddSource(new HardDiskSource(currentMod.Name, currentMod.RootPath));
            _vfsService.AddSource(new HardDiskSource("Default", _pathDiscoveryService.Path));

            var editorControl = new EditorControl(/*_textService*/);
            _regionManager.Regions[RegionNames.Main].Add(editorControl);
            _regionManager.Regions[RegionNames.Main].Activate(editorControl);

            var controller = _container.Resolve<IEditorController>();
            if (controller != null)
                controller.Run();
        }
        #endregion
    }
}